
var offset = $(".toolbar-item-weixin").offset();
//加入购物车
$(".add_scar").click(function (event) {
    var addcar = $(this);
    var img = addcar.parent().parent().find('img').attr('src');
    var flyer = $('<img class="u-flyer" src="' + img + '">');
    var tops = $(window).scrollTop()

    flyer.fly({
        start: {
            left: event.pageX, //开始位置（必填）#fly元素会被设置成position: fixed 
            top: event.pageY - tops, //开始位置（必填）
            width: 0, //结束时宽度 
            height: 0 //结束时高度  
        },
        end: {
            left: offset.left, //结束位置（必填） 
            top: offset.top + 40, //结束位置（必填） 
            width: 0, //结束时宽度 
            height: 0 //结束时高度 
        },

    });
});
$('.p_list').click(function () {
    var index = $(this).index() - 1;
    $('.p_list').eq(index).addClass('nav_color').siblings().removeClass('nav_color')
    $('.pre_list').eq(index).show().siblings().hide()
})
$('.orderTab').click(function () {
    var index = $(this).index() - 1
    $('.orderTab').eq(index).addClass('bottom_color').siblings().removeClass('bottom_color')
    $('.order_list').eq(index).show().siblings().hide()
})
//获取用户信息
function getUserInfo(id) {
    $.post('/presnol/getUserInfo/' + id, {}, function (data) {
        if (data.status == 1) {
            var html = '';
            html += '<p>邮箱 : ' + data.result.emil + '<a href="javascript:;"  data-toggle="modal" data-target="#myModal"  style="margin-left:10px">编辑</a></p>';
            html += '<p>昵称 : ' + data.result.nikename + '</p>';
            html += '<p>性别 : ' + data.result.sex + '</p>';
            html += '<p>出生日期 : ' + data.result.birthday + '</p>';
            html += '<p>自我介绍 : ' + data.result.aboutMe + '</p>';
            $('#userinfo').html('');
            $('#userinfo').html(html);
        }
    })
}

//更新用户信息
function updateUser(id) {
    var userNikename = $('#userNikename').val()
    var userBirthday = $('#userBirthday').val()
    var userSex = $('#userSex').val()
    var userAboutme = $('#userAboutme').val()
    if (userNikename != '' && userBirthday != '' && userSex != '' && userAboutme != '') {
        $.post('/presnol/updateUser/' + id, { userNikename: userNikename, userBirthday: userBirthday, userSex: userSex, userAboutme: userAboutme }, function (data) {
            if (data.status == 1) {
                var html = '';
                html += '<p>邮箱 : ' + data.result.emil + '<a href="javascript:;"  data-toggle="modal" data-target="#myModal" style="margin-left:10px">编辑个人资料</a></p>';
                html += '<p>昵称 : ' + data.result.nikename + '</p>';
                html += '<p>性别 : ' + data.result.sex + '</p>';
                html += '<p>出生日期 : ' + data.result.birthday + '</p>';
                html += '<p>自我介绍 : ' + data.result.aboutMe + '</p>';
                $('#userinfo').html('');
                $('#userinfo').html(html);
                layer.msg(data.msg)
            } else {
                layer.msg(data.msg)
            }
        })
    } else {
        layer.msg('请完善修改信息')
    }
}
//获取用户订单
function getOrder() {
    $.post('/presnol/getOrder', {}, function (data) {
        if (data.status == 1) {
            var html = '';
            if (data.result.length == 0) {
                html += '<div class="col-xs-12 col-md-12 line">暂无订单信息,快去购买书籍吧</div>'
            } else {
                for (var i = 0; i < data.result.length; i++) {
                    html += '<div class="col-xs-2 col-md-2 line"><span>' + data.result[i].number + '</span></div>';
                    for (var j = 0; j < data.result[i].info.length; j++) {
                        if (data.result[i].info.length > 1) {
                            html += '<div class="col-xs-2 col-md-2 line"><span>' + data.result[i].info[j].name + '<span> *' + data.result[i].info[j].num + '</span></span>...</div>';
                            html += '<div class="col-xs-1 col-md-1 line"><span>￥' + (data.result[i].info[j].money) * (data.result[i].info[j].num) + '</span>...</div>';
                            break;
                        } else {
                            html += '<div class="col-xs-2 col-md-2 line"><span>' + data.result[i].info[j].name + '<span> *' + data.result[i].info[j].num + '</span></span></div>';
                            html += '<div class="col-xs-1 col-md-1 line"><span>￥' + (data.result[i].info[j].money) * (data.result[i].info[j].num) + '</span></div>';
                        }
                    }
                    html += '<div class="col-xs-4 col-md-4 line"><span>' + data.result[i].create_at + '</span></div>';
                    for (var j = 0; j < data.result[i].info.length; j++) {
                        if (data.result[i].info.length > 1) {
                            html += '<div class="col-xs-1 col-md-1 line"><span>' + data.result[i].info[j].pay_estate + '</span></div>';
                            html += '<div class="col-xs-1 col-md-1 line"><span>' + data.result[i].info[j].order_estate + '</span></div>';
                            break;
                        } else {
                            html += '<div class="col-xs-1 col-md-1 line"><span>' + data.result[i].info[j].pay_estate + '</span></div>';
                            html += '<div class="col-xs-1 col-md-1 line"><span>' + data.result[i].info[j].order_estate + '</span></div>';
                        }
                    }
                    html += '<div class="col-xs-1 col-md-1"><p style="margin-top:35px"><a href="#" class="color" data-toggle="modal" data-target="#orderListModal" onclick="lookOrderList(\'' + data.result[i]._id + '\')">查看</a></p></div>';
                }
            }
            $('#orderList').html('');
            $('#orderList').html(html);
        } else {
            alert(data.result)
        }
    })
}
//查看订单详情
function lookOrderList(id) {
    $.post('/presnol/getOrderDetil', { id: id }, function (data) {
        if (data.status == 1) {
            var html = '';
            html += "<h4>"
            for (var i = 0; i < data.result.length; i++) {
                html += '<div>订单号 : <span>' + data.result[i].number + '</span></div>';
                for (var j = 0; j < data.result[i].info.length; j++) {
                    html += '<div>商品 : <span>' + data.result[i].info[j].name + '<span> *' + data.result[i].info[j].num + '</span></span></div>';
                    html += '<div>价格 : <span>￥' + (data.result[i].info[j].money) * (data.result[i].info[j].num) + '</span></div>';
                }
                html += '<div>时间 : <span>' + data.result[i].create_at + '</span></div>';
                for (var j = 0; j < 1; j++) {
                    html += '<div>付款状态 : <span>' + data.result[i].info[j].pay_estate + '</span></div>';
                    html += '<div>订单状态 : <span>' + data.result[i].info[j].order_estate + '</span></div>';
                }
            }
            html += '</h4>'
            $('#ModalorderList').html('');
            $('#ModalorderList').html(html);
        }
    })
}

//获取地址信息
function getAdress() {
    $.post('/presnol/getAdress', {}, function (data) {
        if (data.status == 1) {
            var html = '';
            if (data.result.length == 0) {
                html += '<div style="height:35px" class="col-xs-12 col-md-12 text-center">暂无地址,请去添加</div>'
            } else {
                for (var i = 0; i < data.result.length; i++) {
                    html += '<div style="height:35px">'
                    html += '<div class="col-xs-1 col-md-1"><span>' + data.result[i].who + '</span></div>';
                    html += '<div class="col-xs-2 col-md-2"><span>' + data.result[i].place + '</span></div>';
                    html += '<div class="col-xs-2 col-md-2"><span>' + data.result[i].detilplace + '</span></div>';
                    html += '<div class="col-xs-1 col-md-1"><span>' + data.result[i].zip_code + '</span></div>';
                    html += '<div class="col-xs-2 col-md-2"><span>' + data.result[i].phone + '</span></div>';
                    html += '<div class="col-xs-2 col-md-2"><span><a href="#" class="text-danger" onclick="deleteadress(\'' + data.result[i]._id + '\')" data-toggle="modal" data-target="#deleteAdressModal">删除</a> | <a href="#" data-toggle="modal" data-target="#addressModal" onclick="updateAdress(\'' + data.result[i]._id + '\')">修改</a></span></div>';
                    if (data.result[i].is_default == "是") {
                        html += '<div class="col-xs-2 col-md-2"><span id="span1">默认地址</span></div>';
                    } else {
                        html += '<div class="col-xs-2 col-md-2"><span id="span2" onclick="layAdress(\'' + data.result[i]._id + '\')">设为默认</span></div>';
                    }
                    html += '</div>'
                }
            }
            $('#adressList').html('');
            $('#adressList').html(html);
        }
    })
}


var adress_id = '';
var lihao = true
function updateAdress(id) {
    adress_id = id;
    lihao = true
}
function addAdress() {
    lihao = false
}
function deleteadress(id) {
    adress_id = id;
}
//增加地址//更新地址
function Adress() {
    var adressWho = $('#adressWho').val();
    var adressPlace = $('#adressPlace').val();
    var aderssPlaceDetil = $('#aderssPlaceDetil').val();
    var adressZipCode = $('#adressZipCode').val();
    var adressPhone = $('#adressPhone').val();
    if (lihao == false) {
        $.post('/presnol/addAdress', { adressWho: adressWho, adressPlace: adressPlace, aderssPlaceDetil: aderssPlaceDetil, adressZipCode: adressZipCode, adressPhone: adressPhone }, function (data) {
            if (data.status == 1) {
                getAdress();
                layer.msg(data.msg)
            } else {
                layer.msg(data.msg)
            }
        })
    }
    if (lihao == true) {
        $.post('/presnol/updateAdress', { id: adress_id, adressWho: adressWho, adressPlace: adressPlace, aderssPlaceDetil: aderssPlaceDetil, adressZipCode: adressZipCode, adressPhone: adressPhone }, function (data) {
            if (data.status == 1) {
                getAdress();
                layer.msg(data.msg)
            } else {
                layer.msg(data.msg)
            }
        })
    }
}

//删除地址
function deleteAdress() {
    $.post('/presnol/deleteAdress', { id: adress_id }, function (data) {
        if (data.status == 1) {
            getAdress();
            layer.msg(data.msg)
        } else {
            layer.msg(data.msg)
        }
    })
}

//设置默认地址
function layAdress(id) {
    $.post('/presnol/layAdress', { id: id }, function (data) {
        if (data.status == 1) {
            getAdress();
            layer.msg(data.msg)
        } else {
            layer.msg(data.msg)
        }
    })
}
//加入购物车
function join(id) {
    $.post('/shopCar/join/' + id, {}, function (data) {
        if (data.status == 1) {
            layer.msg(data.msg)
        } else {
            layer.msg(data.msg)
        }
    })
}
//取消收藏
function cancelCollect(id) {
    $.post('/shopCar/cancelCollect/' + id, {}, function (data) {
        if (data.status == 1) {
            var html = '';
            if (data.result.length < 1) {
                html += '<div class="col-xs-12 col-md-12 text-center color"><h4>你还没有收藏任何书 ! 快去挑选喜欢的书吧 !</h4></div>';
            } else {
                for (var i = 0; i < data.result.length; i++) {
                    html += ' <div class="col-xs-4 col-md-3 book_list">';
                    html += ' <ul class="list-group book_list2">';
                    html += '<li class="list-group-item">';
                    html += '<img src="/images/books/' + data.result[i].book_id.img+'" style="width:180px;height:auto">';
                    html += '<span class="topKuang2">';
                    html += '<a href="javascript:void(0)" onclick="cancelCollect(\'' + data.result[i].book_id._id + '\')">取消收藏</a>';
                    html += '<a href="javascript:void(0)" class="add_scar" onclick="join(\'' + data.result[i].book_id._id + '\')">加入购物车</a>';
                    html += '</span>';
                    html += '</li>';
                    html += '<li class="list-group-item" style="min-height:100px">';
                    html += '<p>' + data.result[i].name + '</p>';
                    html += '<p class="text-muted" style="margin-left:8px">' + data.result[i].author + '</p>';
                    html += '<p class="pull-right color2" style="font-size:18px">￥' + data.result[i].money + '</p>';
                    html += '</li>';
                    html += '</ul>';
                    html += '</div>';
                }
            }
            $('#collectList').html('');
            $('#collectList').html(html);
            layer.msg(data.msg)
        } else {
            layer.msg(data.msg)
        }
    })
}
//验证原密码
var originalpassword=true;
function original_password() {
    var original_password = $('#original_password').val()
    $.post('/presnol/original_password', { original_password: original_password }, function (data) {
        if (data.status == 1) {
            $('#original_password').removeClass('passwordBorder').addClass('passwordBorder2')
            originalpassword = true
        } else {
            $('#original_password').removeClass('passwordBorder2').addClass('passwordBorder')
            originalpassword = false
        }
    })
    if (originalpassword == true){
        return true
    }else{
        return false
    }
}

//验证密码 
function new_password() {
    psd1 = $('#new_password').val();
    var flagZM = false ;
    var flagSZ = false ;
    var flagQT = false ;

    for (i = 0; i < psd1.length; i++) {
        if ((psd1.charAt(i) >= 'A' && psd1.charAt(i) <= 'Z') || (psd1.charAt(i) >= 'a' && psd1.charAt(i) <= 'z')) {
            flagZM = true;
        }
        else if (psd1.charAt(i) >= '0' && psd1.charAt(i) <= '9') {
            flagSZ = true;
        } else {
            flagQT = true;
        }
    }
    if (!flagZM || !flagSZ || flagQT) {
        $('#new_password').removeClass('passwordBorder2').addClass('passwordBorder')
        return false
    } else {
        $('#new_password').removeClass('passwordBorder').addClass('passwordBorder2')
        return true
    }
}

//验证确认密码 
function new_password_two() {
    if ($('#new_password_two').val() != $('#new_password').val() || $('#new_password_two').val() == '') {
        $('#new_password_two').removeClass('passwordBorder2').addClass('passwordBorder')
        return false
    } else {
        $('#new_password_two').removeClass('passwordBorder').addClass('passwordBorder2')
        return true
    }
}

//修改密码
function updatePassword() {
    if (original_password() && new_password() && new_password_two()) {
        $.post('/presnol/updatePassword', { password: $('#new_password_two').val() }, function (data) {
            if (data.status == 1) {
                $('#original_password').val('');
                $('#original_password').removeClass('passwordBorder2');
                $('#new_password').val('');
                $('#new_password').removeClass('passwordBorder2');
                $('#new_password_two').val('')
                $('#new_password_two').removeClass('passwordBorder2');
                layer.msg(data.msg)
            } else {
                layer.msg(data.msg)
            }
        })
    } else {
        return
    }
}